﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Vinno.WMS.Service.Migrations.WMSDb
{
    /// <inheritdoc />
    public partial class init : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.EnsureSchema(
                name: "VINNO_WMS");

            migrationBuilder.CreateTable(
                name: "MaterialCategory",
                schema: "VINNO_WMS",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", maxLength: 128, nullable: false, comment: "编号"),
                    ParentId = table.Column<Guid>(type: "uniqueidentifier", nullable: true, comment: "父编号"),
                    Name = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: false, comment: "类别名称"),
                    FrnName = table.Column<string>(type: "nvarchar(2048)", maxLength: 2048, nullable: true, comment: "英文名称"),
                    Description = table.Column<string>(type: "nvarchar(MAX)", maxLength: 4096, nullable: true, comment: "类别描述"),
                    TreePathString = table.Column<string>(type: "nvarchar(1024)", maxLength: 1024, nullable: false, comment: "节点树形路径")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MaterialCategory", x => x.Id);
                    table.ForeignKey(
                        name: "FK_MaterialCategory_ParentId",
                        column: x => x.ParentId,
                        principalSchema: "VINNO_WMS",
                        principalTable: "MaterialCategory",
                        principalColumn: "Id");
                },
                comment: "物料");

            migrationBuilder.CreateTable(
                name: "WareHouse",
                schema: "VINNO_WMS",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", maxLength: 128, nullable: false, comment: "编号"),
                    Name = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: false, comment: "仓库名称"),
                    Description = table.Column<string>(type: "nvarchar(MAX)", maxLength: 4096, nullable: true, comment: "仓库描述"),
                    CreatedTime = table.Column<DateTime>(type: "datetime2", nullable: false, comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_WareHouse", x => x.Id);
                },
                comment: "仓库信息");

            migrationBuilder.CreateTable(
                name: "IPN",
                schema: "VINNO_WMS",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", maxLength: 128, nullable: false, comment: "编号"),
                    MaterialCategoryId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "MaterialCategoryId"),
                    Status = table.Column<int>(type: "int", nullable: false, comment: "Status(Vinno.WMS.Pack.Data.Status：[0]待申请；[1]研发审批；[2]IPN审批；[3]申请完成)"),
                    IpnNo = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: true, comment: "IpnNo"),
                    Name = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: false, comment: "Name"),
                    Unit = table.Column<int>(type: "int", nullable: false, comment: "Unit"),
                    Description = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: true, comment: "Description"),
                    Manufacturer = table.Column<string>(type: "nvarchar(2048)", maxLength: 2048, nullable: true, comment: "Manufacturer"),
                    StockAttributes = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: false, comment: "StockAttributes"),
                    Project = table.Column<int>(type: "int", nullable: false, comment: "Project"),
                    Remark = table.Column<string>(type: "nvarchar(MAX)", maxLength: 4096, nullable: true, comment: "Remark"),
                    CreatorId = table.Column<long>(type: "bigint", nullable: true, comment: "创建者编号"),
                    CreatedTime = table.Column<DateTime>(type: "datetime2", nullable: false, comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_IPN", x => x.Id);
                    table.ForeignKey(
                        name: "FK_IPN_MaterialCategory_MaterialCategoryId",
                        column: x => x.MaterialCategoryId,
                        principalSchema: "VINNO_WMS",
                        principalTable: "MaterialCategory",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "IPN信息");

            migrationBuilder.CreateTable(
                name: "WareHouseArea",
                schema: "VINNO_WMS",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", maxLength: 128, nullable: false, comment: "编号"),
                    AreaName = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: false, comment: "库区名称"),
                    AreaType = table.Column<int>(type: "int", nullable: false, comment: "库区类型(Vinno.WMS.Pack.Data.WareHouseAreaType：[0]电子物料；[1]机构物料；[2]开发板；[3]竞品；[4]仪器；[5]工具；[6]办公用品)"),
                    WareHouseId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "关联仓库")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_WareHouseArea", x => x.Id);
                    table.ForeignKey(
                        name: "FK_WareHouseArea_WareHouseId",
                        column: x => x.WareHouseId,
                        principalSchema: "VINNO_WMS",
                        principalTable: "WareHouse",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "库区信息");

            migrationBuilder.CreateTable(
                name: "WareHouseAreaPos",
                schema: "VINNO_WMS",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", maxLength: 128, nullable: false, comment: "编号"),
                    AreaPosName = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: true, comment: "库位名称"),
                    StorageNo = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true, comment: "货架号"),
                    LevelNo = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true, comment: "层号"),
                    PosNo = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true, comment: "位号"),
                    WareHouseAreaId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "关联库区")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_WareHouseAreaPos", x => x.Id);
                    table.ForeignKey(
                        name: "FK_WareHouseAreaPos_WareHouseAreaId",
                        column: x => x.WareHouseAreaId,
                        principalSchema: "VINNO_WMS",
                        principalTable: "WareHouseArea",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "库位信息");

            migrationBuilder.CreateTable(
                name: "Material",
                schema: "VINNO_WMS",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", maxLength: 128, nullable: false, comment: "编号"),
                    WareHouseId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "WareHouseId"),
                    WareHouseAreaId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "WareHouseAreaId"),
                    WareHouseAreaPosId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "WareHouseAreaPosId"),
                    IPNId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "IPNId"),
                    Num = table.Column<decimal>(type: "decimal(22,10)", precision: 22, scale: 10, nullable: false, comment: "Num")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Material", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Material_IPN_IPNId",
                        column: x => x.IPNId,
                        principalSchema: "VINNO_WMS",
                        principalTable: "IPN",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_Material_WareHouseAreaId",
                        column: x => x.WareHouseAreaId,
                        principalSchema: "VINNO_WMS",
                        principalTable: "WareHouseArea",
                        principalColumn: "Id");
                    table.ForeignKey(
                        name: "FK_Material_WareHouseAreaPosId",
                        column: x => x.WareHouseAreaPosId,
                        principalSchema: "VINNO_WMS",
                        principalTable: "WareHouseAreaPos",
                        principalColumn: "Id");
                    table.ForeignKey(
                        name: "FK_Material_WareHouseId",
                        column: x => x.WareHouseId,
                        principalSchema: "VINNO_WMS",
                        principalTable: "WareHouse",
                        principalColumn: "Id");
                },
                comment: "物料信息");

            migrationBuilder.CreateTable(
                name: "MaterialInStorage",
                schema: "VINNO_WMS",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", maxLength: 128, nullable: false, comment: "编号"),
                    MaterialId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "MaterialId"),
                    Project = table.Column<int>(type: "int", nullable: false, comment: "Project"),
                    Num = table.Column<decimal>(type: "decimal(22,10)", precision: 22, scale: 10, nullable: false, comment: "Num"),
                    OrderNo = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: false, comment: "OrderNo"),
                    Remark = table.Column<string>(type: "nvarchar(MAX)", maxLength: 4096, nullable: true, comment: "Remark"),
                    HandleStatus = table.Column<int>(type: "int", nullable: false, comment: "HandleStatus(Vinno.WMS.Pack.Data.HandleStatus：[0]未办理；[1]已办理)"),
                    CreatorId = table.Column<long>(type: "bigint", nullable: true, comment: "创建者编号"),
                    CreatedTime = table.Column<DateTime>(type: "datetime2", nullable: false, comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MaterialInStorage", x => x.Id);
                    table.ForeignKey(
                        name: "FK_MaterialInStorage_MaterialId",
                        column: x => x.MaterialId,
                        principalSchema: "VINNO_WMS",
                        principalTable: "Material",
                        principalColumn: "Id");
                },
                comment: "物料入库");

            migrationBuilder.CreateTable(
                name: "MaterialOutStorage",
                schema: "VINNO_WMS",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", maxLength: 128, nullable: false, comment: "编号"),
                    MaterialId = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "MaterialId"),
                    Num = table.Column<decimal>(type: "decimal(22,10)", precision: 22, scale: 10, nullable: false, comment: "Num"),
                    UseTarget = table.Column<string>(type: "nvarchar(MAX)", maxLength: 4096, nullable: true, comment: "UseTarget"),
                    Project = table.Column<int>(type: "int", nullable: false, comment: "Project"),
                    Remark = table.Column<string>(type: "nvarchar(MAX)", maxLength: 4096, nullable: true, comment: "Remark"),
                    Status = table.Column<int>(type: "int", nullable: false, comment: "Status(Vinno.WMS.Pack.Data.OutStorgeStatus：[0]待审批；[1]研发审批；[2]出库完成)"),
                    CreatorId = table.Column<long>(type: "bigint", nullable: true, comment: "创建者编号"),
                    CreatedTime = table.Column<DateTime>(type: "datetime2", nullable: false, comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MaterialOutStorage", x => x.Id);
                    table.ForeignKey(
                        name: "FK_MaterialOutStorage_MaterialId",
                        column: x => x.MaterialId,
                        principalSchema: "VINNO_WMS",
                        principalTable: "Material",
                        principalColumn: "Id");
                },
                comment: "物料出库");

            migrationBuilder.CreateIndex(
                name: "IDX_IPN_IpnNo",
                schema: "VINNO_WMS",
                table: "IPN",
                column: "IpnNo",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IDX_IPN_Name",
                schema: "VINNO_WMS",
                table: "IPN",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_IPN_MaterialCategoryId",
                schema: "VINNO_WMS",
                table: "IPN",
                column: "MaterialCategoryId");

            migrationBuilder.CreateIndex(
                name: "IDX_Material_WareHouseAreaPosIPNId",
                schema: "VINNO_WMS",
                table: "Material",
                columns: new[] { "WareHouseId", "WareHouseAreaId", "WareHouseAreaPosId", "IPNId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Material_IPNId",
                schema: "VINNO_WMS",
                table: "Material",
                column: "IPNId");

            migrationBuilder.CreateIndex(
                name: "IX_Material_WareHouseAreaId",
                schema: "VINNO_WMS",
                table: "Material",
                column: "WareHouseAreaId");

            migrationBuilder.CreateIndex(
                name: "IX_Material_WareHouseAreaPosId",
                schema: "VINNO_WMS",
                table: "Material",
                columns: new[] { "WareHouseId", "WareHouseAreaId", "WareHouseAreaPosId" });

            migrationBuilder.CreateIndex(
                name: "IX_Material_WareHouseAreaPosId1",
                schema: "VINNO_WMS",
                table: "Material",
                column: "WareHouseAreaPosId");

            migrationBuilder.CreateIndex(
                name: "IDX_MaterialCategory_Name",
                schema: "VINNO_WMS",
                table: "MaterialCategory",
                columns: new[] { "Name", "ParentId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IDX_MaterialCategory_TreePathString",
                schema: "VINNO_WMS",
                table: "MaterialCategory",
                column: "TreePathString",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_MaterialCategory_ParentId",
                schema: "VINNO_WMS",
                table: "MaterialCategory",
                column: "ParentId");

            migrationBuilder.CreateIndex(
                name: "IX_MaterialInStorage_MaterialId",
                schema: "VINNO_WMS",
                table: "MaterialInStorage",
                column: "MaterialId");

            migrationBuilder.CreateIndex(
                name: "IX_MaterialOutStorage_MaterialId",
                schema: "VINNO_WMS",
                table: "MaterialOutStorage",
                column: "MaterialId");

            migrationBuilder.CreateIndex(
                name: "IDX_WareHouse_Name",
                schema: "VINNO_WMS",
                table: "WareHouse",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IDX_WareHouseArea_WareHouseAreaName",
                schema: "VINNO_WMS",
                table: "WareHouseArea",
                columns: new[] { "WareHouseId", "AreaName" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IDX_WareHouseAreaPos_WareHouseAreaPosName",
                schema: "VINNO_WMS",
                table: "WareHouseAreaPos",
                columns: new[] { "WareHouseAreaId", "AreaPosName" },
                unique: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "MaterialInStorage",
                schema: "VINNO_WMS");

            migrationBuilder.DropTable(
                name: "MaterialOutStorage",
                schema: "VINNO_WMS");

            migrationBuilder.DropTable(
                name: "Material",
                schema: "VINNO_WMS");

            migrationBuilder.DropTable(
                name: "IPN",
                schema: "VINNO_WMS");

            migrationBuilder.DropTable(
                name: "WareHouseAreaPos",
                schema: "VINNO_WMS");

            migrationBuilder.DropTable(
                name: "MaterialCategory",
                schema: "VINNO_WMS");

            migrationBuilder.DropTable(
                name: "WareHouseArea",
                schema: "VINNO_WMS");

            migrationBuilder.DropTable(
                name: "WareHouse",
                schema: "VINNO_WMS");
        }
    }
}
